package com.ylr.blog.framework.manager;

import com.ylr.blog.common.constant.BlogConstant;
import com.ylr.blog.framework.service.BlogContentService;
import lombok.AllArgsConstructor;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-06-05 10:37:00
 * className: BlogContentManager
 * version: 1.0
 * description:
 */
@Service("blogContentManager")
@AllArgsConstructor
public class BlogContentManager {

    private static final Logger log = LoggerFactory.getLogger(BlogContentManager.class);

    private final BlogContentService blogContentService;

    /**
     * 保存博客正文操作
     * @param blogId  博客主键
     * @param code    博客编号
     * @param content 博客正文
     */
    public void saveBlogContent(Long blogId, String code, String content) {
        try (HintManager hintManager = HintManager.getInstance()) {
            // 设置分库分表的分片键
            hintManager.addDatabaseShardingValue(BlogConstant.BLOG_CONTENT, code);
            hintManager.addTableShardingValue(BlogConstant.BLOG_CONTENT, code);

            blogContentService.saveBlogContent(blogId, content);
        }
    }

    /**
     * 修改博客正文操作
     * @param blogId  博客主键
     * @param code    博客编号
     * @param content 博客正文
     */
    public void updateBlogContent(Long blogId, String code, String content) {
        try (HintManager hintManager = HintManager.getInstance()) {
            // 设置分库分表的分片键
            hintManager.addDatabaseShardingValue(BlogConstant.BLOG_CONTENT, code);
            hintManager.addTableShardingValue(BlogConstant.BLOG_CONTENT, code);

            blogContentService.updateBlogContent(blogId, content);
        }
    }
}
